import Vue from 'vue'
import moment from 'moment'
import Vuelayer from 'vue-layer'
// 山西华为云
// let HOST_URL = "http://121.36.201.171:8080/";
// let FILE_URL = "http://121.36.201.171:8080/wav/";
// let WS_URL = "ws://121.36.201.171:8080/ws/gateway";

// let HOST_URL = "http://39.104.206.213:8890/";
// let FILE_URL = "http://39.104.206.213:8890/wav/";
// let WS_URL = "ws://39.104.206.213:8890/ws/gateway";
import 'vue-layer/lib/vue-layer.css'

// let HOST_URL = "http://121.196.179.23:8890/";
// let FILE_URL = "http://121.196.179.23:8890/wav/";
// let WS_URL = "ws://121.196.179.23:8890/ws/gateway";
const HOST_URL = window.CONST.VUE_APP_API_BASE_URL
const FILE_URL = window.CONST.VUE_APP_API_BASE_FILE_URL
const WS_URL = window.CONST.VUE_APP_WS_URL
// 大连
// let HOST_URL = "http://39.104.136.31:8080/";
// let FILE_URL = "http://39.104.136.31:8080/wav/";
// let WS_URL = "ws://39.104.136.31:8080/ws/gateway";
// 获取地址json
// let HOST_URL = window.CONST.HOST_URL;
// let FILE_URL = window.CONST.FILE_URL;
// let WS_URL = window.CONST.WS_URL;
export default {
  HOST: HOST_URL,
  FILE_URL: FILE_URL,
  WS_URL: WS_URL,
  file (url) {
    return FILE_URL + url
  },
  async post (url, params, options) {
    const response = await Vue.http.post(url, params, options)
    if (response && response.status === 200) {
      return response.body
    } else {
      alert('服务器异常！')
      return null
    }
  },

  async get (url, params, options) {
    const response = await Vue.http.get(url, params, options)
    if (response && response.status === 200) {
      return response.body
    } else {
      alert('服务器异常！')
      return null
    }
  },

  install (Vue) {
    Vue.filter('file', url => this.file(url))
    Vue.filter('time', time => {
      const date = moment(time, 'YYYY-MM-DD HH:mm:ss')
      if (!date.isValid()) return ''
      return date.format('YYYY-MM-DD')
    })
    Vue.filter('percent', value => {
      return value + '%'
    })
    Vue.prototype.file = this.file
    Vue.prototype.$layer = Vuelayer(Vue)
    Vue.http.options.root = HOST_URL
    Vue.http.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
	// Vue.http.headers.common['appKey'] = '3c8611704bafd7999bb7e9d2c46c9c5e7318cd08'
    Vue.http.options.xhr = {
      withCredentials: true
    }
    Vue.http.options.crossOrigin = true
    Vue.http.options.emulateJSON = true
  }
}
